package com.xplay.xpocker.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.xplay.xpocker.room.mahjong.dto.MahjongMessage;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.io.IOException;


@MappedTypes({MahjongMessage.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class MsMetaTypeHandler extends AbstractObjectTypeHandler<MahjongMessage> {

    /**
     *

     <result typeHandler="com.xcode.common.config.MenuMetaTypeHandler" column="meta" javaType="com.xcode.common.core.entity.MenuMeta"
         jdbcType="VARCHAR"
        property="meta"/>

     <if test="meta != null and meta != ''">meta = #{meta,jdbcType=VARCHAR, javaType=com.xcode.common.core.entity.MenuMeta, typeHandler=com.xcode.common.config.MenuMetaTypeHandler},</if>

     *
     */
    private static ObjectMapper objectMapper = null;
    private Class<?> type;

    static {
        objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        //反序列化的时候如果多了其他属性,不抛出异常
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public MsMetaTypeHandler(Class<?> type) {
        this.type = type;
    }
    
    protected Object parse(String json) {
        try {
            return objectMapper.readValue(json, this.type);
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }
    
    protected String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException var3) {
            throw new RuntimeException(var3);
        }
    }

}